package com.leoman.adsindex.service.impl;

import com.leoman.adsindex.dao.AdsIndexDao;
import com.leoman.adsindex.entity.AdsIndex;
import com.leoman.adsindex.service.AdsIndexService;
import com.leoman.category.dao.CategoryDao;
import com.leoman.category.entity.Category;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.Configue;
import com.leoman.entity.Constant;
import com.leoman.post.dao.PostBaseDao;
import com.leoman.post.entity.PostBase;
import com.leoman.utils.ImageUtil;
import com.leoman.welfare.dao.WelfareDao;
import com.leoman.welfare.entity.Welfare;
import com.leoman.work.dao.WorkComicDao;
import com.leoman.work.dao.WorkDao;
import com.leoman.work.dao.WorkNovelDao;
import com.leoman.work.dao.WorkVideoDao;
import com.leoman.work.entity.Work;
import com.leoman.work.entity.WorkComic;
import com.leoman.work.entity.WorkNovel;
import com.leoman.work.entity.WorkVideo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;

/**
 * Created by 蔡琦 on 2016/7/6 0006.
 */
@Service
public class AdsIndexServiceImpl extends GenericManagerImpl<AdsIndex, AdsIndexDao> implements AdsIndexService {

    @Autowired
    private AdsIndexDao adsIndexDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private PostBaseDao postBaseDao;

    @Autowired
    private WorkDao workDao;

    @Autowired
    private WorkVideoDao workVideoDao;

    @Autowired
    private WorkNovelDao workNovelDao;

    @Autowired
    private WorkComicDao workComicDao;

    @Autowired
    private WelfareDao welfareDao;

    @Override
    public Page<AdsIndex> page(Integer pageNum, Integer pageSize) {
//        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id");
//        Page<AdsIndex> page = adsIndexDao.findAll(new Specification<AdsIndex>() {
//            @Override
//            public Predicate toPredicate(Root<AdsIndex> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                Predicate result = null;
//                List<Predicate> predicateList = new ArrayList<Predicate>();
//
//                if (predicateList.size() > 0) {
//                    result = cb.and(predicateList.toArray(new Predicate[]{}));
//                }
//
//                if (result != null) {
//                    query.where(result);
//                }
//                return query.getGroupRestriction();
//            }
//
//        }, pageRequest);
        return adsIndexDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));

    }

    @Override
    public void getInfo(Long adsIndexId, Model model) {
        AdsIndex adsIndex = adsIndexDao.findOne(adsIndexId);
        if (StringUtils.isNotEmpty(adsIndex.getCover())) {
            adsIndex.setCover(ImageUtil.clearUploadUrl(adsIndex.getCover()));
        }
        model.addAttribute("adsIndex", adsIndex);

        if (adsIndex.getPosition().equals(Constant.POSITION_000)) {

            // 查询帖子分类
            if (adsIndex.getCategoryId() != null) {
                Category category = categoryDao.findOne(adsIndex.getCategoryId());
                model.addAttribute("category", category);
            }

            // 查询帖子
            if (adsIndex.getWorkId() != null) {
                PostBase postBase = postBaseDao.findOne(adsIndex.getWorkId());
                model.addAttribute("postBase", postBase);
            }

        } else if (adsIndex.getPosition().equals(Constant.POSITION_001)) {

            // 查询视频分类
            if (adsIndex.getCategoryId() != null) {
                Work work = workDao.findOne(adsIndex.getCategoryId());
                model.addAttribute("work", work);
            }

            // 查询视频续集
            if (adsIndex.getWorkId() != null) {
                WorkVideo workVideo = workVideoDao.findOne(adsIndex.getWorkId());
                model.addAttribute("workVideo", workVideo);
            }

        } else if (adsIndex.getPosition().equals(Constant.POSITION_002)) {

            // 查询小说分类
            if (adsIndex.getCategoryId() != null) {
                Work work = workDao.findOne(adsIndex.getCategoryId());
                model.addAttribute("work", work);
            }

            // 查询小说章节
            if (adsIndex.getWorkId() != null) {
                WorkNovel workNovel = workNovelDao.findOne(adsIndex.getWorkId());
                model.addAttribute("workNovel", workNovel);
            }

        } else if (adsIndex.getPosition().equals(Constant.POSITION_003)) {

            // 查询漫画分类
            if (adsIndex.getCategoryId() != null) {
                Work work = workDao.findOne(adsIndex.getCategoryId());
                model.addAttribute("work", work);
            }

            // 查询漫画话集
            if (adsIndex.getWorkId() != null) {
                WorkComic workComic = workComicDao.findOne(adsIndex.getWorkId());
                model.addAttribute("workComic", workComic);
            }

        } else if (adsIndex.getPosition().equals(Constant.POSITION_004)) {

            // 查询资源
            String type = "";
            if (adsIndex.getCategoryId().equals(Constant.RESOURCE_TYPE_001)) {
                type = "剧照";
            } else if (adsIndex.getCategoryId().equals(Constant.RESOURCE_TYPE_002)) {
                type = "音乐";
            } else if (adsIndex.getCategoryId().equals(Constant.RESOURCE_TYPE_003)) {
                type = "游戏";
            }
            model.addAttribute("type", type);

        } else if (adsIndex.getPosition().equals(Constant.POSITION_005)) {

            // 查询福利社
            String type = "";
            if (adsIndex.getCategoryId() == 0) {
                type = "铃声";
            } else if (adsIndex.getCategoryId() == 1) {
                type = "经验值";
            } else if (adsIndex.getCategoryId() == 2) {
                type = "实物";
            } else if (adsIndex.getCategoryId() == 3) {
                type = "表情包";
            } else if (adsIndex.getCategoryId() == 4) {
                type = "商城购买";
            } else if (adsIndex.getCategoryId() == 5) {
                type = "游戏兑换码";
            }

            model.addAttribute("type", type);
            if (adsIndex.getWorkId() != null) {
                Welfare welfare = welfareDao.findOne(adsIndex.getWorkId());
                model.addAttribute("welfare", welfare);
            }
        }
    }

    @Override
    public List<AdsIndex> findByTypeAndCategoryId(Integer type, Long categoryId) {
        return adsIndexDao.findByTypeAndCategoryId(type, categoryId);
    }
}
